package org.unicat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.unicat.mapper.UserInfoMapper;
import org.unicat.model.User;
import org.unicat.mapper.UserMapper;

@Controller // 控制器注解
//@RequestMapping("/login") // 类中的请求映射注解
public class UserController { // 控制器类

    //自动装配Mybatis User映射器
    @Autowired
    UserMapper usermapper;

    @Autowired
    UserInfoMapper userInfoMapper;

    @RequestMapping("/")
    public String index(Model model){
        return "redirect:/login/index";
    }

    @RequestMapping("/login/index")
    public String login(Model model){
        return "index";
    }

    @RequestMapping("/login/check")
    public String hello(User user_test, Model model) {
        //视图模型对象初始化
        ModelAndView modelAndView = new ModelAndView();

        System.out.println(user_test.getUsername());
        User user = (User) usermapper.selectOne(user_test.getUsername());

        if (user_test.getPassword().equals(user.getPassword())) {

            modelAndView.addObject(user);

            //权限查看
            System.out.println(userInfoMapper.selectOne(user.getUsername()));
            String indentity = userInfoMapper.selectOne(user.getUsername()).getIdentity();
            switch (indentity){
                case "student":
                    return "";

                case "admin":
                    System.out.println("管理员登录");
                    return "adminManage";

                default:
                    return "";
            }
        } else {
            model.addAttribute("msg", "登录密码或用户名错误！！");
            return "error";
        }
    }

//    //查询全部用户
//    @RequestMapping("/selectall")
//    public String select(Model model) {
//        //ModelAndView modelAndView = new ModelAndView();
//        List<User> user_list = usermapper.selectAll();
//		/*for(User user:user_list)
//			System.out.println(user.getId()+" "+user.getName()+" "+user.getPassword());*/
//        model.addAttribute("user_list", user_list);
//        return "showList";
//    }
//
//    //插入一条记录的录入视图
//    @RequestMapping("/adduser")
//    public String addUser() {
//        return "AddUser";
//    }
//
//    //插入一条记录
//    @RequestMapping("/insert")
//    public String insert(User user) {
//        usermapper.addUser(user);
//        return "hello";
//    }
//
//    @RequestMapping("/delete")
//    public String delete(User user) {
//        usermapper.deleteUser(user);
//        return "hello";
//    }
//
//    @RequestMapping("/update")
//    public String update(User user) {
//        usermapper.updateUser(user);
//        return "hello";
//    }

	/*
	@RequestMapping("/check") // 方法层级的请求注解 {var}是占位符
	public ModelAndView hello(@PathVariable int id,@PathVariable String name){
		  // 路径参数注解，常用在REST风格的编程中
         // 视图模型对象初始化
		ModelAndView modelAndView = new ModelAndView();
		User user = new User(id,name);
		modelAndView.setViewName("hello");
		modelAndView.addObject(user);
		return modelAndView;// 返回视图模型
		}



	@RequestMapping("/test") // 方法层级的请求注解
	public ModelAndView helloUser(User user){// 映射方法  参数自动装箱
         // 视图模型对象初始化
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("hello");
		modelAndView.addObject(user);
		return modelAndView;// 返回视图模型
	}

	@GetMapping("/hellomatrix/{username}")
	public ModelAndView helloMatrix(
			@PathVariable String username,
			@MatrixVariable(name="userid",pathVar="username") String userid){
				// 视图模型对象初始化
		        System.out.println("UserName:"+username);
		        System.out.println("UserID:"+userid);
				ModelAndView modelAndView = new ModelAndView();
				//modelAndView.setViewName("hello");
				return modelAndView;// 返回视图模型
	}

	// GET /pets/42;q=11;r=22

	@GetMapping("/pets/{petId}")
	public void findPet(@PathVariable String petId, @MatrixVariable int q,@MatrixVariable int r) {
		System.out.println("PetID:"+petId);
        System.out.println("Q:"+q);
        System.out.println("R:"+r);
	    // petId == 42
	    // q == 11
	}*/

}
